package com.example.demo.thread.ch5;

import com.example.demo.thread.util.Debug;
import com.example.demo.thread.util.Tools;

import java.util.concurrent.CountDownLatch;

/**
 * 一个线程多次执行CountDownLatch.countDown()
 *
 * @author: htc
 * @date: Created in 14:53 2019/1/14.
 */
public class CountDownLatchExample {
    private static final CountDownLatch latch = new CountDownLatch(4);
    private static int data;

    public static void main(String[] args) throws InterruptedException {
        Thread workerThread = new Thread() {
            @Override
            public void run() {
                for (int i = 1; i <= 10; i++) {
                    data = i;
                    latch.countDown();
                    //使当前线程暂停一会
                    Tools.randomPause(1000);
                    System.out.println("this is i =" + i);
                }
            }

            ;
        };
        workerThread.start();
        latch.await();
        Debug.info("It's done. data = %d", data);
    }
}
